package flc.ast.fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.FragmentUtils;
import com.blankj.utilcode.util.PathUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.huawei.hms.videoeditor.ui.mediapick.activity.MediaPickActivity;
import com.huawei.hms.videoeditor.ui.template.module.TemplateHomeFragment;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import flc.ast.R;
import flc.ast.activity.RecordActivity;
import flc.ast.activity.SelVideoActivity;
import flc.ast.adapter.HomeRecordAdapter;
import flc.ast.bean.MyVideoBean;
import flc.ast.databinding.FragmentHomeBinding;
import flc.ast.utils.Content;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.MediaUtil;
import stark.common.basic.utils.StkPermissionHelper;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HomeFragment extends BaseNoModelFragment<FragmentHomeBinding> {

    private List<MyVideoBean> listShow = new ArrayList<>();
    private HomeRecordAdapter recordAdapter;

    @Override
    protected int onCreate() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);

        mDataBinding.ivVideoEdit.setOnClickListener(this);
        mDataBinding.tvVideoSpeed.setOnClickListener(this);
        mDataBinding.tvVideoUpend.setOnClickListener(this);
        mDataBinding.tvAddText.setOnClickListener(this);
        mDataBinding.tvAddSticker.setOnClickListener(this);
        mDataBinding.tvCreateRecordAll.setOnClickListener(this);

        mDataBinding.rvRecordList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        recordAdapter = new HomeRecordAdapter();
        mDataBinding.rvRecordList.setAdapter(recordAdapter);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivVideoEdit://进入编辑
                getPermission();
                break;
            case R.id.tvVideoSpeed://视频变速
                goSelPermission(0);
                break;
            case R.id.tvVideoUpend://视频倒放
                goSelPermission(1);
                break;
            case R.id.tvAddText://添加文字
                goSelPermission(2);
                break;
            case R.id.tvAddSticker://添加贴纸
                goSelPermission(3);
                break;
            case R.id.tvCreateRecordAll://查看全部
                startActivity(RecordActivity.class);
                break;
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onResume() {
        super.onResume();
        getData();
    }

    private void getData() {
        listShow.clear();
        //获取文件夹下文件
        String path = PathUtils.getFilesPathExternalFirst() + Content.myRecord;
        List<File> files = FileUtils.listFilesInDir(path);
        if (files.size() > 0) {
            for (File file : files) {
                listShow.add(new MyVideoBean(file.getPath(), file.getName(), MediaUtil.getDuration(file.getPath()), FileUtils.getFileLastModified(file.getPath())));
            }
        }
        if (listShow.size() > 0) {
            if (listShow.size() > 3) {
                listShow = listShow.subList(0, 3);
            }
            recordAdapter.setList(listShow);
            mDataBinding.rvRecordList.setVisibility(View.VISIBLE);
            mDataBinding.llRecordNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvRecordList.setVisibility(View.GONE);
            mDataBinding.llRecordNoData.setVisibility(View.VISIBLE);
        }
    }

    //去到选择素材界面
    private void getPermission() {
        StkPermissionHelper.permission(StkPermissionHelper.Permission.ACCESS_IMAGE_VIDEO)
                .reqPermissionDesc(getString(R.string.get_storage_permission))
                .callback(new StkPermissionHelper.ACallback() {
                    @Override
                    public void onGranted() {
                        startActivity(MediaPickActivity.class);
                    }

                }).request();
    }

    private void goSelPermission(int type) {
        StkPermissionHelper.permission(StkPermissionHelper.Permission.ACCESS_IMAGE_VIDEO)
                .reqPermissionDesc(getString(R.string.get_storage_permission))
                .callback(new StkPermissionHelper.ACallback() {
                    @Override
                    public void onGranted() {
                        SelVideoActivity.type = type;
                        startActivity(SelVideoActivity.class);
                    }
                }).request();
    }

}